package cn.net.isir.service2;

import cn.net.isir.dao.MinipIndexDataDao;
import cn.net.isir.dao.ShopDao;
import cn.net.isir.entity.Shop;
import cn.net.isir.entity.TimeRange;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author liuxubo
 * @filename MinipIndexDataService.java
 * @package cn.net.isir.service
 * @email lxb@isir.net.cn
 * @date 2019/03/01
 * @comment
 * @since 1.8
 */
@Service
public class MinipIndexDataService {
    @Resource
    private MinipIndexDataDao minipIndexDataDao;
    @Resource
    private ShopDao shopDao;

    /**
     * 获取首页数据
     *
     * @param salesmanId 销售人员ID
     * @return 结果
     */
    public Map getIndexData(String salesmanId, String turnedShopIds, List<TimeRange> timeRangeList) {
        Map<String, Object> map = new HashMap<>();
        // 月流水
        map.put("monthAmount", minipIndexDataDao.queryMonthFlow(salesmanId, turnedShopIds, timeRangeList));
        Map dayMap = minipIndexDataDao.queryDayFlow(salesmanId, turnedShopIds, timeRangeList);
        // 日流水
        map.put("dayAmount", dayMap.get("dayFlow"));
        // 日订单数
        map.put("dayOrderNum", dayMap.get("dayOrderCount"));
        // 未转出店铺数
        map.put("noTurnShopNum", minipIndexDataDao.noTurnedShop(salesmanId, turnedShopIds));
        // 可转出店铺数
        map.put("canTurnShopNum", minipIndexDataDao.monthCanTurnShops(salesmanId, turnedShopIds, timeRangeList));
        // 达到日活店铺
        map.put("dayLiveNum", minipIndexDataDao.dayLiveCount(salesmanId, turnedShopIds, timeRangeList));
        // 日活未达标店铺
        map.put("dayNoLiveNum", minipIndexDataDao.dayNotLiveCount(salesmanId, turnedShopIds, timeRangeList));
        return map;
    }


    /**
     * 根据销售人员+时间段+开始结束时间获取总流水
     *
     * @param salesman  销售人员以及有效时间
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 流水
     */
    public BigDecimal queryIntervalFlow(Map<String, List<TimeRange>> salesman, String turnedShopIds, Date startTime, Date endTime) {
        return minipIndexDataDao.queryIntervalFlow(salesman, turnedShopIds, startTime, endTime);
    }

    /**
     * 根据销售人员+时间段+开始结束时间获取总流水
     *
     * @param salesman  销售人员以及有效时间
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 流水
     */
    public BigDecimal queryShopIntervalFlow(List<TimeRange> salesman, String shopId, Date startTime, Date endTime) {
        return minipIndexDataDao.queryShopIntervalFlow(salesman, shopId, startTime, endTime);
    }

    /**
     * 获取未结转店铺列表
     *
     * @param salesmanId 当前登录用户id
     * @return 店铺列表
     */
    public List<Map<String, Object>> getNoTurnShopList(String salesmanId, String turnedShopIds, List<TimeRange> timeRangeList) {
        List<Map<String, Object>> list = new ArrayList<>();
        //获取到未结转店铺的店名和创建时间
        List<Shop> shops = minipIndexDataDao.noTurnShopList(salesmanId, turnedShopIds);
        // 获取到所有未结转店铺的流水信息
        List<Map<String, Object>> shopInfos = minipIndexDataDao.queryShopDayFlow(salesmanId, turnedShopIds, timeRangeList);

        Map<String, Object> map;
        for (Shop shop : shops) {
            map = new HashMap<>();
            map.put("id", shop.getId());
            //获取店铺名称
            map.put("shopName", shop.getName());
            //获取创建时间
            map.put("createTime", shop.getCreateTime());
            // 获取日订单数量和日流水 并放入map
            for (Map<String, Object> shopInfo : shopInfos) {
                if(shopInfo.get("shopId")!=null){
                    if (shopInfo.get("shopId").equals(shop.getId())) {
                        //获取店铺日订单数
                        map.put("dayOrderNum", shopInfo.get("dayOrderNum"));
                        //获取店铺日流水
                        map.put("dayAmount", shopInfo.get("dayAmount"));
                        break;
                    }
                }
            }
            // 放入具体某个店铺的信息
            list.add(map);
        }
        // 返回多个店铺的数据
        return list;
    }

    /**
     * 获取本月每一天的订单流水和订单数
     *
     * @param shopId        店铺id
     * @param salesmanId    销售人员id
     * @param turnedShopIds 已转出店铺列表
     * @param timeRangeList 销售人员
     * @return 每天的流水和订单
     */
    public List<Map> getEveryDayFlow(String shopId, String salesmanId, String turnedShopIds, List<TimeRange> timeRangeList) {
        Shop shop = shopDao.selectByPrimaryKey(shopId);

        List<Map> mapList = minipIndexDataDao.queryEveryDayFlow(shopId, salesmanId, turnedShopIds, timeRangeList);
        Calendar nowCal = Calendar.getInstance();
        // 年
        int year = nowCal.get(Calendar.YEAR);
        // 月
        int monthIndex = nowCal.get(Calendar.MONTH) + 1;
        String month = monthIndex < 10 ? "0" + monthIndex : String.valueOf(monthIndex);
//        int maxDate = nowCal.getActualMaximum(Calendar.DATE);
        int maxDate = nowCal.get(Calendar.DAY_OF_MONTH);
        // 年-月
        String year_month = year + "-" + month;

        List<Map> everyDay = new ArrayList<>();
        for (int i = 1; i <= maxDate; i++) {
            // 年-月-日
            String date = year_month + "-" + (i < 10 ? "0" + i : i);
            boolean isExist = false;
            for (Map map : mapList) {
                if (map.get("createTime").equals(date)) {
                    map.put("shopName", shop.getName());
                    everyDay.add(map);
                    isExist = true;
                    break;
                }
            }
            if (!isExist) {
                Map map = new HashMap();
                map.put("dayAmount", 0);
                map.put("dayOrderNum", 0);
                map.put("shopName", shop.getName());
                map.put("createTime", date);
                everyDay.add(map);
            }
        }
        return everyDay;
    }

    /**
     * 日活未达标店铺列表查询
     *
     * @return
     */
    public List<Map<String,Object>> getDayNoLiveShopList(String salesmanId, String turnedShopIds, List<TimeRange> timeRangeList) {
        return minipIndexDataDao.getDayNoLiveShopList(salesmanId, turnedShopIds, timeRangeList);
    }
}
